//
//  CatagoryCell.m
//  Kugou-OC
//
//  Created by BH on 16/6/14.
//  Copyright © 2016年 Sunny. All rights reserved.
//

#import "CatagoryCell.h"
#import "GlobalDefine.h"
#import "CatagoryModel.h"
#import <SDAutoLayout/UIView+SDAutoLayout.h>

@implementation CatagoryCell

+(instancetype)cellWithTableView:(UITableView *)tableView{
    
//    static NSString *identifier = @"CatagoryCell";
//    CatagoryCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//    if (cell == nil) {
//        cell = [[CatagoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
//    }
    
    CatagoryCell *cell = [[CatagoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
    }
    
    return self;
}

-(void)setIndexPath:(NSIndexPath *)indexPath{
    _indexPath = indexPath;
    
    self.catagoryBtn = nil;
    for (int i=0; i<self.children.count; i++) {
        UIButton *button = ALLOC(UIButton);
        [button setTitle:[self.children[i] childrenName] forState:UIControlStateNormal];
        button.titleLabel.font = TITLE_FOUR_FONT;
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setBackgroundImage:[self imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
        [button setBackgroundImage:[self imageWithColor:BG_COLOR] forState:UIControlStateHighlighted];
        button.layer.borderWidth = 1;
        button.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
        if ([self.children[i] is_hot]) {
            [button setImage:IMAGE(@"catagory_hot") forState:UIControlStateNormal];
        }

        if ([self.children[i] is_new]) {
            [button setImage:IMAGE(@"catagory_new") forState:UIControlStateNormal];
        }
        [self.contentView addSubview:button];
        
        //每行显示button的个数
        NSInteger count;
        //button宽度
        CGFloat btnWidth;
        if (indexPath.section == 0) {
            count = i%2;
            btnWidth = (SCREEN_WIDTH-30)/2;
        }else{
            count = i%4;
            btnWidth = (SCREEN_WIDTH-30)/4;
        }
        
        //自动布局
        if (count != 0) {
            //第一列
            button.sd_layout.leftSpaceToView(self.catagoryBtn,0).topEqualToView(self.catagoryBtn).bottomEqualToView(self.catagoryBtn).widthIs(self.catagoryBtn.width);
            
        }else{
            if (self.catagoryBtn) {
                //第2,3,4...所有列
                button.sd_layout.leftSpaceToView(self.contentView,15).topSpaceToView(self.catagoryBtn,0).widthIs(self.catagoryBtn.width).heightIs(30);
                
            }else{
                //第一个按钮
                button.sd_layout.leftSpaceToView(self.contentView,15).topSpaceToView(self.contentView,5).widthIs(btnWidth).heightIs(30);
            }
        }
        self.catagoryBtn = button;
    }
}

//  颜色转换为背景图片
- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end
